import { TouchControls } from "../libs/TouchControls";
//
import { Sphere } from "./Sphere";
import { Logo } from "./Logo";

//
export function Scene(renderer, props = {}) {
    //
    THREE.Object3D.call(this);

    // add
    renderer.add(this);

    // visible
    this.visible = false;

    // sphere
    this.add(this.sphere = new Sphere(props.sphereSize));
    // logo
    this.add(this.logo = new Logo(props.logoSize));
    // position
    this.logo.position.z = -props.sphereSize * 0.9;

    // controls
    this.controls = new TouchControls(this, renderer.domElement);
    // update
    this.controls.on('update', object => {
        //
        this.logo.rotation.z = -object.rotation.z;
    });

    // autoplay
    this.autoplay = this.controls.autoplay.init();


    // loadPanoramic
    this.loadPanoramic = (texture) => {
        this.sphere.material.map = texture;
        this.sphere.material.needsUpdate = true;
        return this;
    }

    // loadLogo
    this.loadLogo = (texture) => {
        //
        this.logo.material.map = texture;
        this.logo.material.needsUpdate = true;
        //
        this.logo.scale.set(1, 1, 1);
    };

    //
    this.clearPanoramic = () => {
        this.sphere.material.map = null;
        this.sphere.material.needsUpdate = true;
        return this;
    };

    //
    this.clearLogo = () => {
        //
        this.logo.material.map = null;
        this.logo.material.needsUpdate = true;
        //
        this.logo.scale.set(0.00001, 0.00001, 0.00001);
        return this;
    };


    // hide
    this.hide = (duration = 0) => {

        if (duration) {
            //
            this.controls.animate({ opacity: [1, 0], z: [0, props.sphereSize * 0.5] }, duration, (res) => {
                //
                this.sphere.material.opacity = res.opacity;
                this.sphere.material.needsUpdate = true;
                //
                this.logo.material.opacity = res.opacity;
                this.logo.material.needsUpdate = true;
                //
                this.position.z = res.z;

            }, () => {
                //
                this.visible = false;
                //
                this.sphere.material.opacity = 1;
                this.sphere.material.needsUpdate = true;
                //
                this.logo.material.opacity = 1;
                this.logo.material.needsUpdate = true;
                //
                this.position.z = 0;
            }, false);
        }
        else {
            this.visible = false;
        }

        return this;
    };

    // show
    this.show = (duration = 0) => {

        if (duration) {
            //
            this.visible = true;
            //
            this.controls.animate({ opacity: [0, 1], z: [props.sphereSize * 0.1, 0] }, duration, (res) => {
                //
                this.visible = true;
                //
                this.sphere.material.opacity = res.opacity;
                this.sphere.material.needsUpdate = true;
                //
                this.logo.material.opacity = res.opacity;
                this.logo.material.needsUpdate = true;
                //
                this.position.z = -res.z;

            }, () => {
                //
                this.sphere.material.opacity = 1;
                this.sphere.material.needsUpdate = true;
                //
                this.logo.material.opacity = 1;
                this.logo.material.needsUpdate = true;
                //
                this.position.z = 0;
            }, false);
        }
        else {
            this.visible = true;
        }

        return this;
    }

}
//
Scene.prototype = Object.create(THREE.Object3D.prototype);